package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;

import org.compiere.model.Mhrmmedicare;
import org.compiere.model.Mhrmmedicareline;
import org.compiere.model.Mhrmmedicarelinelist;
import org.compiere.model.Mhrmsalaryemployee;
import org.compiere.model.X_hrm_catalog;
import org.compiere.model.X_hrm_medicare;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import extend.org.compiere.hrm.TaxCompute;
import extend.org.compiere.hrm.TaxVariable;
import extend.org.compiere.hrm.TypePayment;
import extend.org.compiere.utils.Util;

public class GenerateMedicarePaymentLine extends SvrProcess {
	
	private Mhrmmedicareline medicareline = null;
	private Mhrmmedicarelinelist medicarelist = null;
	private TaxVariable taxcomp = null;
	Timestamp dateFrom = null;
	double SUM = .0;
	
	/** Set calendar */
	Calendar cMonth = Calendar.getInstance();

	@Override
	protected void prepare() {
		
		 medicareline = new Mhrmmedicareline(getCtx(), getRecord_ID(), null);		

	}

	@Override
	protected String doIt() throws Exception {
		Mhrmmedicare medicare = new Mhrmmedicare(getCtx(), medicareline.gethrm_medicare_ID(), null);
		dateFrom = medicareline.getStartDate();
		   int Month = Util.getMonth(medicareline.getStartDate(), medicareline.getEndDate());
		   for (int i = 0; i <= Month; i++){
			   cMonth.setTime(dateFrom);
			   
			   medicarelist = Mhrmmedicarelinelist.getMedicareList(medicareline.getC_BPartner_ID(), dateFrom, medicareline.get_ID());
			   if (medicarelist == null)
				   medicarelist = new Mhrmmedicarelinelist(getCtx(), 0, null);
			   medicarelist.sethrm_medicare_line_ID(medicareline.gethrm_medicare_line_ID());
			   medicarelist.setC_BPartner_ID(medicareline.getC_BPartner_ID());
			   medicarelist.setDateFrom(dateFrom);
			   medicarelist.sethrm_medicaretype(medicare.gethrm_medicaretype());
			   BigDecimal DIV = medicareline.gethrm_total().divide(new BigDecimal(Month+1), BigDecimal.ROUND_UP);
			   medicarelist.sethrm_total(DIV);
			   medicarelist.saveEx();
			   SUM = DIV.doubleValue();

				/** Generate employee tax */
			   String type = TypePayment.MEDICARE;
			   if(medicare.gethrm_medicaretype().equals(X_hrm_medicare.HRM_MEDICARETYPE_Profit)) 
				   type = TypePayment.PROFIT;
			   taxcomp =  TaxCompute.GetTax(medicareline.getC_BPartner_ID(), SUM, dateFrom, type);
								
				BigDecimal amount = Mhrmsalaryemployee.getFullSalary(medicareline.getC_BPartner_ID(), dateFrom);
				if (amount.compareTo(new BigDecimal(0)) == 0){
					medicarelist.sethrm_IPN(new BigDecimal(0));
					medicarelist.sethrm_amount(new BigDecimal(taxcomp.getTOTAL() + taxcomp.getIPN()));
				}else{
					medicarelist.sethrm_IPN(new BigDecimal(taxcomp.getIPN()));
					medicarelist.sethrm_amount(new BigDecimal(taxcomp.getTOTAL()));
				}	
				medicarelist.sethrm_amount(new BigDecimal(medicareline.gethrm_total().doubleValue()-(taxcomp.getIPN()+taxcomp.getOPV())));
				medicarelist.sethrm_OPV(new BigDecimal(taxcomp.getOPV()));
				medicarelist.sethrm_CNalog(new BigDecimal(taxcomp.getCNALOG()));
				medicarelist.sethrm_COtch(new BigDecimal(taxcomp.getCOTCH()));
			   	medicarelist.saveEx();
			   	
			    cMonth.add(Calendar.MONTH, 1);
			    dateFrom = new Timestamp(cMonth.getTimeInMillis());
			    
			}
		   
		  medicareline.saveEx();
	
		return Msg.translate(Env.getCtx(), "Success");
	}

}
